// pages/doctordetails/doctordetails.js
let wxparse = require("../../wxParse/wxParse.js");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    doctorscheduleList: [],
    divisionId: '',
    isShowModel: false, //控制弹窗是否显示，默认不显示
    isShowConfirm: false, //是否只显示确定按钮，默认不是
    ModelId: 0, //弹窗id
    ModelTitle: '', //弹窗标题
    readtimes: '',
    isDisabled: true
  },
  //阻断模态弹窗点击穿透
  preventTouchMove: function () {},
  //调用模态弹窗
  showModel: function (e) {
    //将传过来的标题和内容展示到弹窗上
    this.setData({
      isShowModel: true,
      ModelId: e.ModelId,
      ModelTitle: e.ModelTitle
    })
  },
  //确定事件
  confirm: function (e) {
    if (e.currentTarget.dataset.modelid == 0) {
      console.log("用户点击了我已阅读")
    }
    //关闭模态弹窗
    this.setData({
      isShowModel: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.divisionId)
    var that = this;
    that.setData({
      divisionId: options.divisionId
    });
    wx.showLoading({
      title: '数据加载中',
      icon: 'loading'
    });
    wx.request({
      url: that.data.myHttp + '/fuyou/getScheduleList',
      data: {
        divisionId: that.data.divisionId,
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: (res) => {
        if (res.data.obj !== null) {
          if (res.data.obj.scheduledList.length > 0) {
            var ModelContent = res.data.obj.description;
            if (ModelContent) {
              wxparse.wxParse('ModelContent', 'html', ModelContent, that, 5)
              this.showModel({
                ModelId: 0,
                ModelTitle: '挂号须知'
              })
              //倒计时
              this.readCount(10)
            }
            this.setData({
              doctorscheduleList: res.data.obj.scheduledList,
            })
          }
        }
      },
      complete: () => {
        wx.hideLoading()
      }
    })

  },

  bindyuyue: function (event) {
    var user = wx.getStorageSync("user")
    var index = event.currentTarget.dataset.bindex;
    if (user == null || user == '' || user == undefined) {
      wx.showModal({
        title: '提示',
        content: '预约前请先登录',
        confirmText: '登录',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.navigateTo({
              url: '../login/login',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      if (!this.data.doctorscheduleList[index].reservation) {
        this.toast('预约已满');
        return false;
      }
      var data = {
        hospitalName: '硚口区妇幼保健院',
        divisionName: this.data.doctorscheduleList[index].divisionName || '-',
        scheduledList: this.data.doctorscheduleList[index],
      }
      wx.navigateTo({
        url: '../qiaokou_appointmentInfo/qiaokou_appointmentInfo?qiaokouAppointmentInfo=' + encodeURIComponent(JSON.stringify(data)) + '&appointmentDayTimeNum=' + index
      })
    }
  },
  //10秒倒计时
  readCount(time) {
    if (time >= 0) {
      this.setData({
        readtimes: time,
      })
      setTimeout(this.readCount, 1000, time - 1)
    } else {
      this.setData({
        isDisabled: false
      })
    }
  },
  toast(img) {
    wx.showToast({
      title: img,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})